package com.onec.service.customer.service.impl;

import com.onec.service.api.enums.StatusEnum;
import com.onec.service.api.enums.error.MerchantErrorEnum;
import com.onec.service.api.exception.CustomerException;
import com.onec.service.customer.dao.agent.AgentFeeRepository;
import com.onec.service.customer.entity.agent.AgentFee;
import com.onec.service.customer.service.AgentFeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;


/**
 * @author onec
 * @see
 * @since 20200107
 */
@Service
public class AgentFeeServiceImpl implements AgentFeeService {

    @Autowired
    AgentFeeRepository agentFeeRepository;

    @Override
    public AgentFee findAgentFeeByAgentIdAndProduct(Long agentId, String businessNo) {
        return agentFeeRepository.findByAgentIdAndBusinessNo(agentId, businessNo);
    }

    @Override
    public AgentFee getAvailableAgentFee(Long agentId, String businessNo) {
        AgentFee agentFee = this.findAgentFeeByAgentIdAndProduct(agentId, businessNo);
        if (Optional.ofNullable(agentFee).isEmpty()) {
            throw new CustomerException(MerchantErrorEnum.PRODUCT_AGENT_ERROR, "");
        }
        if (!StatusEnum.isSuccess(agentFee.getStatus())) {
            throw new CustomerException(MerchantErrorEnum.PRODUCT_AGENT_ERROR_FAILED, agentFee.getStatus().name());
        }
        return agentFee;
    }
}
